<?php

namespace App\Http\Controllers\Admin;
header('Content-Type:text/html;charset=utf-8');


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Route,URL,Auth;


class BzController extends Controller
{
	public function index(Request $request){

		if(Auth::guard('admin')->user()->role_id == 22){
			$map['bz.type'] = '1';
		}else{
			$map = array();
		}

		if($request->rows) {
			$res = DB::table('bz')
				->where($map)
				->join('admin_users', 'bz.user_id', '=', 'admin_users.id')
				->select('bz.*','admin_users.name')
				->orderBy('time','desc')
				->paginate($request->rows);
			$list = $res->items();
			$result['total'] = $res->total();
			$result['rows'] = $list;
			echo json_encode($result);
		}else{
			return view('admin.bz.index');
		}
	}

	public function addbz(Request $request){

		 if($request->isMethod('post')){
            $input = $request->all();
            $data = Input::except('_token');


            $data = array(
                'title' => $input['title'],
                'content' => $input['content'],
                'time' => $input['time'],
				'type' => $input['type'],
                'user_id' => Auth::guard('admin')->user()->id,
                'created_at' => date('Y-m-d H:i:s',time()),

            );

            $abg = DB::table('bz')->insert($data);
            if ($abg) {
                 return ['status'=>1,'msg'=>'添加成功'];
            }else{
                 return ['status'=>0,'msg'=>'添加失败'];
            }
           
        }


	 	return view('admin.bz.addbz');


		
	}


	public function bzedit($id,Request $request){

		if($request->isMethod('post')){

            $input = $request->all();
            $data = Input::except('_token');
                 
            $datas = array(
              	'title' => $input['title'],
                'content' => $input['content'],
                'time' => $input['time'],
				'type' => $input['type'],
                'user_id' => Auth::guard('admin')->user()->id,
                'update_at' => date('Y-m-d H:i:s',time()),
            );

	            $upjq = DB::table('bz')->where('id',$id)->update($datas);

	            if ($upjq) {
	                 return ['status'=>1,'msg'=>'修改成功'];
	            }else{
	                 return ['status'=>0,'msg'=>'修改失败'];
	            }

	        }
	 	



          $map['id'] = $id;
	 	  $re = DB::table('bz')->where($map)->first();
	 	  return view('admin.bz.bzedit', ['id'=>$id,'re'=>$re]);


	}

	public function del($id,Request $request){

        $re = db::table('bz')->where('id',$id)->delete();
        if($re){
            return ['status'=>1,'msg'=>'删除成功'];
        }else{
            return ['status'=>0,'msg'=>'删除失败'];
        }
    }


}
